#
# Copyright (c) Microsoft Corporation. All rights reserved. See license.txt for license information.
#
##
# Contains the DependencyWalker class
#
# Date:   2008-10-28 16:19:13
#

from moffile import MofFile

##
# This class knows how to translate cim class
# dependencies to generate a list of filenames
# that must be included for a functional
# repository.
#
class DependencyWalker:
    ##
    # Constructor
    # Recursively builds up the list of files in the mof repository
    # that must be included in the final repository in order for
    # all the dependencies off all mof files in moffiles to be sattisfied.
    #
    def __init__(self, mofrepository, moffiles):
        self.mofRepository = mofrepository
        self.files = []
        for moffile in moffiles:
            self.RecursiveGetRequiredFiles(moffile)
                
    ##
    # Recursively builds up the list of files in the mof repository
    # that must be included in the final repository in order for
    # all the dependencies of moffile to be sattisfied.
    #
    def RecursiveGetRequiredFiles(self, moffile):
        cimclasses = moffile.GetDependentClasses()
        for cimclass in cimclasses:
            definingFile = self.mofRepository.GetFileDefiningClass(cimclass)
            if definingFile and definingFile not in self.files:
                self.RecursiveGetRequiredFiles(definingFile)
                self.files.append(definingFile)

    ##
    # Return the list of mof files generated by the constructor as
    # a list of MofFile objects.
    #
    def GetRequiredFiles(self):
        return self.files
